package com.xgd.phylw.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xgd.phylw.bean.Member;
import com.xgd.phylw.bean.Orderd;
import com.xgd.phylw.dto.OrderScheduleMovieHallDto;
import com.xgd.phylw.service.BackPageService;
import com.xgd.phylw.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private BackPageService backPageService;

    @RequestMapping("ReturnTicket")
    public String returnTicket(Integer orderId, Model model,@RequestParam(value = "pageSize", required = false, defaultValue = "7") int pageSize
            ,@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum) throws ParseException {
        Orderd orderd = new Orderd();
        orderd.setOrderId(orderId);
        orderd.setOrderReturn(1);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        orderd.setReturnTime(sdf.parse(sdf.format(date)));
        int i = orderService.returnTicketByOrder(orderd);
        if(i == 1){
            Page<Object> objectPage = PageHelper.startPage(pageNum,pageSize);
            List<Orderd> orderds = backPageService.SelectAllOrderd();

            model.addAttribute("pages",objectPage.toPageInfo());
            model.addAttribute("orderds",orderds);
            return "order";
        }
        return "order";
    }

    @RequestMapping("DeleteOrder")
    public String deleteOrder(int[] checkId, Model model,@RequestParam(value = "pageSize", required = false, defaultValue = "7") int pageSize
            ,@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum){
        int[] checkIdArray = checkId;
        System.out.println(checkId);
        int i = orderService.deleteByOrder(checkIdArray);
        System.out.println(i);
        Page<Object> objectPage = PageHelper.startPage(pageNum,pageSize);
        List<Orderd> orderds = backPageService.SelectAllOrderd();

        model.addAttribute("pages",objectPage.toPageInfo());
        model.addAttribute("orderds",orderds);
        return "order";
    }

    @RequestMapping("DeleteOrderId")
    public String deleteOrderId(int orderId,Model model,@RequestParam(value = "pageSize",required = false,defaultValue = "7") int pageSize
            ,@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum){
        int[] checkIdArray = new int[1];
        checkIdArray[0] = orderId;
        System.out.println(checkIdArray);
        int i = orderService.deleteByOrder(checkIdArray);
        Page<Object> objectPage = PageHelper.startPage(pageNum,pageSize);
        List<Orderd> orderds = backPageService.SelectAllOrderd();

        model.addAttribute("pages",objectPage.toPageInfo());
        model.addAttribute("orderds",orderds);
        return "order";
    }


    //购票信息
    @RequestMapping("/buyHistory")
    public String buyTicketMessage(@SessionAttribute("user") Member name, Model model){
        List<OrderScheduleMovieHallDto> dt = orderService.selectByMemberName(name.getMemberName());
        model.addAttribute("dt",dt);
        return "my-movie";
    }


    //购票信息
    @RequestMapping("/likeOrder")
    public String likeOrder(String val,Model model
            ,@RequestParam(value = "pageSize",required = false,defaultValue = "6") int pageSize
            ,@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum){

        Page<Object> objectPage = PageHelper.startPage(pageNum,pageSize);
        List<Orderd> orderds =orderService.Order(val);

        model.addAttribute("pages",objectPage.toPageInfo());
        model.addAttribute("orderds",orderds);
        return "order";


    }
}
